/**
 * TtPackDao.java
 * Created at 2017-2-20
 * Created by shihui
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.monitor.dao.impl;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.bean.PageBean;
import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.rms.basic.model.TmPackSerial;
import com.broadtext.rms.monitor.dao.ITtPackDao;
import com.broadtext.rms.monitor.model.FenceGpsInfo;
import com.broadtext.rms.monitor.model.PackLocationInfo;
import com.broadtext.rms.monitor.model.PackRunTimeSpan;
import com.broadtext.rms.monitor.model.TtPack;
import com.broadtext.rms.monitor.model.TtPackCurrent;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: TtPackDao</p>
 * <p>Description: 监控电池清单Dao层</p>
 * <p>Author: shihui</p>
 * <p>Date: 2017-2-20</p>
 */
@Repository("ttPackDao")
public class TtPackDao extends EntityDao<TtPack> implements ITtPackDao {

    /**
     * 
     * <p>Description: 查询电池监控数据</p>
     * @param map 查询条件
     * @param pageBounds 翻页
     * @return 结果
     * @throws DaoException 异常
     */
    @SuppressWarnings("rawtypes")
    @Override
    public EntityPageBean findPackMonitorData(Map<String, Object> map, PageBounds pageBounds) throws DaoException {
        return super.findEntityPageByProperty("com.broadtext.rms.monitor.model.TtPack.findMonitorData",
                map, pageBounds);
    }
    
    
    /**
     * 
     * <p>Description: 根据条件获取当前监控电池实时信息</p>
     * @param sql sqlId
     * @param entity 实体
     * @return 结果集
     * @throws DaoException 异常
     */
    public Map<String, Object> getMonitorDataInfoCurrent(String sql, TtPackCurrent entity) throws DaoException {
        List<Map<String, Object>> list;
        list = this.getCurrentSession().selectList(sql, entity);
        if(list != null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }
    
    /**
     * 
     * <p>Description: 获取对应电池下包含的模块号</p>
     * @param t 查询实体
     * @param sql sqlId
     * @return 结果集
     * @throws DaoException 异常
     */
    public List<String> getNoNums(String sql, Map<String, Object> paramsMap) throws DaoException {
        return this.getCurrentSession().selectList(sql, paramsMap);
    }
    /**
     * 
     * <p>Description: 检查无效状态</p>
     * @param ids 主键数组
     * @return 结果集
     * @throws DaoException 异常
     */
    public Object checkEnableStatus(String[] ids) throws DaoException {
        String result = "";
        List<TtPack> packList;
        packList = this.getCurrentSession().selectList("com.broadtext.rms.monitor.model.TtPack.checkEnableStatus",ids);
        if(packList.isEmpty()){
            //只包含未使用状态返回true;
            result = "true";
        } else {
            //待删除电池中包含其他状态返回false;
            result = "false";
        }
        return result;
    }
    
    /**
     * 
     * <p>Description: 检查掉电状态</p>
     * @param params 查询条件
     * @return 结果
     * @throws DaoException 异常
     */
    public Object checkPowerFailStatus(Map<String, Object> params) throws DaoException {
        String result = "";
        long countResult = 0;
        countResult = this.getCurrentSession().selectOne("com.broadtext.rms.monitor.model.TtPack.checkPowerFailStatus", params);
        if(countResult != 0){
            //上电; 
            result = "true";
        } else {
            //掉电;
            result = "false";
        }
        return result;
    }
    
    /**
     * 
     * <p>Description: 检查电池报废状态</p>
     * @param ids 主键集
     * @return 检查结果
     * @throws DaoException 异常
     */
    public Object checkFailureStatus(String[] ids) throws DaoException {
        String result = "";
        List<TtPack> packList;
        packList = this.getCurrentSession().selectList("com.broadtext.rms.monitor.model.TtPack.getPackByIds",ids);
        if(packList.isEmpty()){
            //只包含未使用状态返回true;
            result = "false";
        } else {
            //待删除电池中包含其他状态返回false;
            result = "true";
        }
        return result;
    }
    
    /**
     * 
     * <p>Description: 报废</p>
     * @param ids 主键数组
     * @param currentUser 当前用户
     * @throws DaoException 异常
     */
    @Override
    public void setFailure(String[] ids, String currentUser) throws DaoException {
        for (String id : ids) {
            TtPack t;
            t = new TtPack();
            t.setPkId(id);
            t.setUpdateDate(new Date());
            t.setUpdateUser(currentUser);
            //1 报废
            t.setPackStatus("1");
            this.getCurrentSession().update("com.broadtext.rms.monitor.model.TtPack.updateByPrimaryKeySelective", t);
            //更新电池信息为报废
            TmPackSerial tp;
            tp = new TmPackSerial();
            tp.setUpdateDate(new Date());
            tp.setUpdateUser(currentUser);
            //用tt_pack id查出对应的电池pack_code
            tp.setSerialNo(id);
            //2 报废
            tp.setStatus("2");
            this.getCurrentSession().update("com.broadtext.rms.basic.model.TmPackSerial.updateBySerialNoSelective", tp);
        }
    }
    
    /**
     * 
     * <p>Description: 查询历史报警列表</p>
     * @param map 查询条件
     * @param pageBounds 分页
     * @return 结果集
     * @throws DaoException 异常
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public EntityPageBean findHistoryWarningList(Map<String, Object> map, PageBounds pageBounds) throws DaoException {
        return super.findEntityPageByProperty("com.broadtext.rms.monitor.model.TtPackAlert.findHistoryWarningList",
                map, pageBounds);
    }
    
    /**
     * 
     * <p>Description: 分页查询 监控电池历史数据</p>
     * @param map 查询条件
     * @param pageBounds 分页
     * @return 结果
     * @throws DaoException 
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public EntityPageBean findMonitorHistoryDataList(Map<String, Object> map, PageBounds pageBounds) throws DaoException {
        return super.findEntityPageByProperty("com.broadtext.rms.monitor.model.TtPackHis.findMonitorHistoryDataList",
                map, pageBounds);
    }
    
    /**
     * 
     * <p>Description: 根据查询条件获取电池历史数据详情</p>
     * @param sql 查询sqlid
     * @param queryMap 查询条件
     * @return 结果集
     * @throws DaoException 异常
     */
    public Map<String, Object> findPackHistoryDataDetail(String sql, Map<String, Object> queryMap) throws DaoException {
        List<Map<String, Object>> list;
        list = this.getCurrentSession().selectList(sql, queryMap);
        if(list != null && !list.isEmpty()){
            return list.get(0);
        }
        return null;
    }
    
    /**
     * 
     * <p>Description: 历史轨迹列表分页查询</p>
     * @param sql 查询sql
     * @param map 查询调价
     * @param pageBounds  分页条件
     * @return 结果集
     * @throws DaoException 异常
     */
    public EntityPageBean<PackLocationInfo> findHistoryLocationListByPage(String sql, Map<String, Object> map, PageBounds pageBounds) throws DaoException {
        PageBean pageBean;
        pageBean = super.findPageByQuery(sql, map, pageBounds);
        EntityPageBean<PackLocationInfo> entityPageBean;
        entityPageBean = new EntityPageBean<PackLocationInfo>();
        List<PackLocationInfo> results;
        results = new LinkedList<PackLocationInfo>();
        List list;
        list = pageBean.getResults();
        for (Object o : list) {
            PackLocationInfo t = (PackLocationInfo) o;
            results.add(t);
        }
        entityPageBean.setCurrentPage(pageBean.getCurrentPage());
        entityPageBean.setTotalCount(pageBean.getTotalCount());
        entityPageBean.setTotalPage(pageBean.getTotalPage());
        entityPageBean.setResults(results);
        entityPageBean.setiTotalDisplayRecords(results.size());
        entityPageBean.setiTotalRecords(pageBean.getTotalCount());
        return entityPageBean;
    }
    
    
    /**
     * 
     * <p>Description: 查询当前累计运行时间状态list</p>
     * @param sql sqlid
     * @param entity 查询条件
     * @return 结果集
     * @throws DaoException 异常
     */
    public List<PackRunTimeSpan> getCurrentRuntimeSpanList(String sql, TtPackCurrent entity) throws DaoException {
        return this.getCurrentSession().selectList(sql, entity);
    }
    
    /**
     * 
     * <p>Description: 根据电池id 终端id获取监控电池实时对象</p>
     * @param queryMap 查询条件
     * @return 结果集
     * @throws DaoException 异常
     */
    public TtPackCurrent getTtPackCurrentById(Map<String, Object> queryMap) throws DaoException {
        List<TtPackCurrent> tpcList;
        tpcList =  this.getCurrentSession().selectList("com.broadtext.rms.monitor.model.TtPackCurrent.getTtPackCurrentById", queryMap);
        if(null != tpcList && !tpcList.isEmpty()){
            return tpcList.get(0);
        } else {
            return null;
        }
    }
    
    /**
     * 
     * <p>Description: 统计是否存在告警信息</p>
     * @param queryMap 查询条件
     * @return 结果集
     * @throws DaoException 异常
     */
    public String countAlertInfo(Map<String, Object> queryMap) throws DaoException {
        long count = 0;
        count = this.getCurrentSession().selectOne("com.broadtext.rms.monitor.model.TtPackAlert.countVolAlertInfo", queryMap);
        if(count > 0) {
            return "yes";
        } else {
            return "no";
        }
    }

    /**
     * 
     * <p>Description: 检查离线文件是否已经上传过</p>
     * @param fileNameLists 离线文件名list
     * @return 结果
     * @throws DaoException 异常
     */
    public Object checkOfflineFileExist(List<String> fileNameLists) throws DaoException {
        long count = 0;
        count =  this.getCurrentSession().selectOne("com.broadtext.rms.monitor.model.TtPack.countOfflineFileNums", fileNameLists);
        if(count == 0) {
            return "success";
        } else {
            return "failure";
        }
    }
    
    /**
     * 删除旧数据
     * @param fenceGpsInfo 参数
     * @throws DaoException 异常
     */
	public void deleteFenceInfo(List<FenceGpsInfo> dataList) throws DaoException {
		this.getCurrentSession().delete("com.broadtext.rms.monitor.model.TtPack.deleteFenceInfo", dataList.get(0));
		this.getCurrentSession().insert("com.broadtext.rms.monitor.model.TtPack.insertFenceInfo", dataList);
	}

}
